package com.dz.lm.web.controller.newuser.video;

import com.alibaba.fastjson.JSONObject;
import com.dz.lm.common.dto.AttachDTO;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.common.util.StringUtil;
import com.dz.lm.common.util.TecentVideo;
import com.dz.lm.common.utils.APIV3SignDemo;
import com.dz.lm.common.utils.Signature;
import com.dz.lm.common.utils.json.JsonUtils;
import com.dz.lm.service.attach.AttachService;
import com.dz.lm.web.controller.BasicControl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Vector;

/**
 * 上传视频控制层
 *
 * @version 1.8;
 * @author: LDY;
 * @Description;
 * @Date: Created in 2020/1/4 15:11;
 */
@Controller
@RequestMapping(value = "/h5/weijie/rewriteUrl/viedo", produces = "application/json")
public class VdeoController extends BasicControl {
    @Autowired
    private AttachService attachService;

    /**
     * 获取视频上传签名
     *
     * @return
     * @throws
     */
    @ResponseBody
    @RequestMapping("/getSignature")
    public Map<String, Object> getSignature(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "获取签名失败";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        long time = System.currentTimeMillis() / 1000;
        int random = new Random().nextInt(Integer.MAX_VALUE);
        String signature = "";
        Signature sign = new Signature();
        sign.setSecretId("AKIDB9sNdeI0LrAE3ePC8rXzDBvQghUEn14s");
        sign.setSecretKey("kGWYVEhutd9PSRDl4A0cy6B1bw2tVryv");
        sign.setCurrentTime(time);
        sign.setRandom(random);
        sign.setSignValidDuration(3600 * 24 * 2);
        try {
            signature = sign.getUploadSignature();
            message = "获取成功";
            result = "00";
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("currentTimeStamp", time);
            retData.put("random", random);
            retData.put("signature", signature);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("currentTimeStamp", time);
            subjo.put("random", random);
            subjo.put("signature", signature);
            return subjo;
        }
    }

    /**
     * s删除签名
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("getSign")
    public Map<String, Object> getSing(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        //参数校验
        Vector<String> inparam = new Vector<String>();
        for (String in : inparam) {
            if (!param.containsKey(in)) {
                subjo = new JSONObject();
                subjo.put("result", "01");
                subjo.put("message", "参数" + in + "异常");
                return subjo;
            }
        }
        String fileId = param.get("fileId") != null ? param.get("fileId").toString() : "";
        Map sign = new HashMap();
        try {
            sign = APIV3SignDemo.getSinature(fileId);
            message = "获取成功";
            result = "00";
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.putAll(sign);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.putAll(sign);
            return subjo;
        }
    }

    /**
     * s删除签名
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("delete")
    public Map<String, Object> delete(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "操作失败";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        //参数校验
        Vector<String> inparam = new Vector<String>();
        for (String in : inparam) {
            if (!param.containsKey(in)) {
                subjo = new JSONObject();
                subjo.put("result", "01");
                subjo.put("message", "参数" + in + "异常");
                return subjo;
            }
        }
        int attachId = param.get("attachId") != null ? StringUtil.getInt(param.get("attachId").toString(), 0) : 0;
        try {
            AttachDTO attach = attachService.sleletById(attachId);
            if (attach != null) {
                try {
                    int i = TecentVideo.delete(attach.getFileId());
                    if (i > 0) {
                        int delete = attachService.delete(attach.getAttachId());
                        if(delete>0){
                            message = "删除成功";
                            result = "00";
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            return subjo;
        }
    }
}
